package com.easydict.resolver;

import com.easydict.config.DictProvider;
import com.easydict.spi.DictionaryResolutionContext;
import com.easydict.spi.DictionaryResolver;

import java.util.Optional;

/**
 * 委托字典解析器
 * 将新的解析器接口适配到旧的DictProvider接口
 */
public class DelegatingDictionaryResolver implements DictionaryResolver {
    
    private static final String RESOLVER_NAME = "DelegatingResolver";
    private static final int DEFAULT_PRIORITY = 1000;
    
    private final DictProvider delegateProvider;
    private final ResolutionMetrics metrics;
    
    public DelegatingDictionaryResolver(DictProvider delegateProvider) {
        this.delegateProvider = delegateProvider;
        this.metrics = new ResolutionMetrics();
    }
    
    @Override
    public Optional<String> resolve(DictionaryResolutionContext context) {
        long startTime = System.nanoTime();
        try {
            metrics.incrementAttempts();
            
            String result = delegateProvider.getDictName(
                    context.getTypeCode(), 
                    context.getStringValue()
            );
            
            if (result != null) {
                metrics.incrementSuccesses();
                return Optional.of(result);
            } else {
                metrics.incrementMisses();
                return Optional.empty();
            }
            
        } catch (Exception e) {
            metrics.incrementErrors();
            return Optional.empty();
        } finally {
            long duration = System.nanoTime() - startTime;
            metrics.recordDuration(duration);
        }
    }
    
    @Override
    public int getPriority() {
        return DEFAULT_PRIORITY;
    }
    
    @Override
    public boolean supports(DictionaryResolutionContext context) {
        return context != null 
                && context.getTypeCode() != null 
                && context.getStringValue() != null;
    }
    
    @Override
    public String getName() {
        return RESOLVER_NAME;
    }
    
    public ResolutionMetrics getMetrics() {
        return metrics;
    }
    
    /**
     * 解析指标统计
     */
    public static class ResolutionMetrics {
        private volatile long attempts = 0;
        private volatile long successes = 0;
        private volatile long misses = 0;
        private volatile long errors = 0;
        private volatile long totalDuration = 0;
        
        public void incrementAttempts() {
            attempts++;
        }
        
        public void incrementSuccesses() {
            successes++;
        }
        
        public void incrementMisses() {
            misses++;
        }
        
        public void incrementErrors() {
            errors++;
        }
        
        public void recordDuration(long nanos) {
            totalDuration += nanos;
        }
        
        public long getAttempts() {
            return attempts;
        }
        
        public long getSuccesses() {
            return successes;
        }
        
        public long getMisses() {
            return misses;
        }
        
        public long getErrors() {
            return errors;
        }
        
        public double getSuccessRate() {
            return attempts > 0 ? (double) successes / attempts : 0.0;
        }
        
        public double getAverageDurationNanos() {
            return attempts > 0 ? (double) totalDuration / attempts : 0.0;
        }
        
        public void reset() {
            attempts = 0;
            successes = 0;
            misses = 0;
            errors = 0;
            totalDuration = 0;
        }
    }
}
